import request from './request'
import type {
  LoginParams,
  PhoneLoginParams,
  VerificationCodeParams,
  UserListResponse,
  CreateAdminParams,
  UserInfo
} from '@/types/user'

// 用户相关API
export const userApi = {
  // 管理员登录
  adminLogin: (data: LoginParams) => {
    return request.post('/users/admin/login/', data)
  },

  // 手机号登录
  phoneLogin: (data: PhoneLoginParams) => {
    return request.post('/users/login/', data)
  },

  // 获取验证码
  getVerificationCode: (data: VerificationCodeParams) => {
    return request.post('/users/generate-code/', data, { 
      headers: { 
        'X-No-Auth': 'true' 
      } 
    } as any)
  },

  // 登出
  logout: (refreshToken: string) => {
    return request.post('/users/logout/', { refresh_token: refreshToken }, { 
      headers: { 
        'X-No-Auth': 'true' 
      } 
    } as any)
  },

  // 获取用户信息
  getProfile: () => {
    return request.get('/users/profile/')
  },

  // 更新用户信息
  updateProfile: (data: Partial<UserInfo>) => {
    return request.put('/users/profile/', data)
  },

  // 更新用户头像
  updateAvatar: (avatar: string) => {
    return request.patch('/users/profile/', { avatar })
  },

  // 获取用户列表（管理员）
  getUserList: (params?: { page?: number; page_size?: number }) => {
    return request.get<UserListResponse>('/users/users/', { params })
  },

  // 获取用户详情
  getUserDetail: (id: number) => {
    return request.get<UserInfo>(`/users/users/${id}/`)
  },

  // 创建管理员
  createAdmin: (data: CreateAdminParams) => {
    return request.post('/users/admin/create/', data)
  },

  // 更新用户状态
  updateUserStatus: (id: number, isActive: boolean) => {
    return request.patch(`/users/users/${id}/`, { is_active: isActive })
  },

  // 删除用户
  deleteUser: (id: number) => {
    return request.delete(`/users/users/${id}/`)
  },

  // 刷新token
  refreshToken: (refreshToken: string) => {
    return request.post('/token/refresh/', { refresh: refreshToken }, { 
      headers: { 
        'X-No-Auth': 'true' 
      } 
    } as any)
  }
}